import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'  // 确保引入store

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'dashboard',
    component: () => import('../views/dashboard.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/signIn',
    name: 'signIn',
    component: () => import('../views/signIn.vue')
  },
  {
    path:'/index',
    name:'index',
    component:()=>import('../views/indexList.vue')
  },
  {
    path:'/FaceUpload',
    name:'FaceUpload',
    component:()=>import('../views/FaceUpload.vue')
  },
  {
    path:'/FacialRecognitionResults',
    name:'FacialRecognitionResults',
    component:()=>import('../views/FacialRecognitionResults.vue')
  },
  {
    path:'/FacialDetection',
    name:'FacialDetection',
    component:()=>import('../views/FacialDetection.vue')
  },
  {
    path:'/ChangePassword',
    name:'ChangePassword',
    component:()=>import('../views/ChangePassword.vue')
  },
  {
    path:'/Setting',
    name:'Setting',
    component:()=>import('../views/Setting.vue')
  },
  {
    path:'/HelpAndFeedback',
    name:'HelpAndFeedback',
    component:()=>import('../views/HelpAndFeedback.vue')
  },
  {
    path:'/AdminOperationPlatform',
    name:'AdminOperationPlatform',
    component:()=>import('../views/AdminOperationPlatform.vue')
  },
  {
    path:'/FaceCompare',
    name:'FaceCompare',
    component:()=>import('../views/FaceCompare.vue')
  },
  {
    path:'/LiveDetection',
    name:'LiveDetection',
    component:()=>import('../views/LiveDetection.vue')
  }
]

// 白名单
const whiteList = ['/login', '/signIn']

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.path)) {
    next();
  } else {
    const token = store.getters.accessToken;
    if (token) {
      next();
    } else {
      next('/login');
    }
  }
});

export default router
